<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <ImportExcel
          type="primary"
          :url="APIS.url"
          plain
          icon="Plus"
          @success="getList"
        ></ImportExcel>
      </el-col>
     
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain @click="handlePush(pushUnit)"
          >推送分析单元</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain @click="handlePush(pushControl)"
          >推送风险事件</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain @click="handlePush(pushEvent)"
          >推送管控措施</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #default>
        <el-table-column
          label="分析单元个数"
          align="center"
          key="unitCount"
          prop="unitCount"
          width="120"
        />
        <el-table-column
          label="风险事件个数"
          align="center"
          key="eventCount"
          prop="eventCount"
          width="120"
        />
        <el-table-column
          label="管控措施个数"
          align="center"
          key="controlCount"
          prop="controlCount"
          width="120"
        />
        <el-table-column
          label="排查任务数"
          align="center"
          key="taskCount"
          prop="taskCount"
          width="100"
        />
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="edit"
          @click="handleEdit(row)"
          v-hasPermi="['preventionSecurityRisk:update']"
          >编辑</el-button
        >
      </template>
      <template #dangerNames="{ row }">
        {{ row.dangerName }}
      </template>
    </Table>
    <el-dialog v-model="outerVisible" width="90%" title="风险分析清单明细">
      <template #default>
        <Inner ref="inner" :parentNode="parentNode"></Inner>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import { apis as APIS } from "@/api/tool/zzgl.js";
import { pushUnit, pushControl, pushEvent } from "@/api/yhpc/push";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import { deptTreeSelect } from "@/api/system/user";
import Inner from "./components/inner.vue";
import { nextTick } from "vue";

const { proxy } = getCurrentInstance();
const inner = ref();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["deviceName", "dangerName", "dangerLevel"].includes(i.prop)
  ),
});
const showSearch = ref(true);
const table = ref();
const parentNode = ref({});
const outerVisible = ref(false);
const handleEdit = (row) => {
  outerVisible.value = true;
  nextTick().then(() => {
    parentNode.value.id = row.id;
    inner.value.changeParent(row);
  });
};

const { fetchData, getList, queryParams } = useAsyncData({
  APIS,
  formJson,
  table,
  proxy,
});
const deptTree = ref([]);
onMounted(() => {
  deptTreeSelect().then((res) => {
    deptTree.value = res.data;
  });
});

const handlePush = (api) => {
  api().then((res) => {
    if (res.msg.includes("成功")) {
      proxy.$modal.msgSuccess(res.msg);
    } else {
      proxy.$modal.msgError(res.msg);
    }
  });
};
</script>

<style lang="scss" scoped></style>
